<template>
  <grid title="评论管理">
    <el-form inline
             style="width: 100%; text-align: left"
             label-width="60px"
             @submit.native.prevent="onSubmit"
             @reset.native.prevent="onReset"
             size="small">
      <el-form-item label="内容">
        <el-input v-model="query.q"
                  placeholder="请输入关键词"
                  clearable></el-input>
      </el-form-item>
      <el-form-item label="文章标题"
                    label-width="80px">
        <el-input v-model="query.title"
                  placeholder="请输入关键词"
                  clearable></el-input>
      </el-form-item>
      <el-form-item label="用户昵称"
                    label-width="80px">
        <el-input v-model="query.user"
                  placeholder="请输入关键词"
                  clearable></el-input>
      </el-form-item>

      <el-form-item label="发布状态"
                    label-width="80px">
        <el-select v-model="query.status">
          <el-option label="全部"
                     value="all"></el-option>
          <el-option label="未发布"
                     value="down"></el-option>
          <el-option label="发布中"
                     value="up"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary"
                   native-type="submit">筛选</el-button>
        <el-button native-type="reset">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="list"
              border
              :resizable="false"
              stripe>
      <el-table-column type="index"
                       label="编号"
                       width="60"
                       align="center"></el-table-column>
      <el-table-column prop="avatar"
                       label="用户头像"
                       width="80"
                       align="center">
        <template slot-scope="scope">
          <user-popover :user="scope.row.user"></user-popover>
        </template>
      </el-table-column>
      <el-table-column prop="content"
                       label="内容">
      </el-table-column>
      <el-table-column prop="title"
                       label="标题">
        <template slot-scope="scope">
          <a>{{ scope.row.item.title }}</a>
        </template>
      </el-table-column>
      <el-table-column prop="favor"
                       label="点赞数">
      </el-table-column>
      <el-table-column prop="status"
                       label="状态">
        <template slot-scope="scope">
          <normal-status :data="scope.row"></normal-status>
        </template>
      </el-table-column>
      <el-table-column prop="created_at"
                       label="创建时间">
        <template slot-scope="scope">
          {{scope.row.created_at | timeFilter }}
        </template>
      </el-table-column>
      <el-table-column fixed="right"
                       label="操作"
                       width="240">
        <template slot-scope="scope">
          <el-button type="primary"
                     @click="editRow(scope.row.id)"
                     size="small"
                     icon="el-icon-edit"></el-button>
          <el-button type="danger"
                     @click="deleteRow(scope.row.id)"
                     size="small"
                     icon="el-icon-delete"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination layout="total, prev, pager, next"
                   background
                   @current-change="fetch"
                   :current-page.sync="query.page"
                   :page-size="query.limit"
                   hide-on-single-page
                   style="display: flex;justify-content: center;"
                   :total="total">
    </el-pagination>
    <el-dialog title="更新评论"
               :visible.sync="dialogVisible"
               width="650px"
               :show-close="false"
               center>
      <el-form ref="dialogForm"
               :model="form"
               label-width="120px"
               :rules="rules">
        <el-form-item label="内容"
                      prop="content">
          <el-input v-model="form.content"
                    autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="是否显示">
          <el-switch v-model="form.status"
                     active-text="显示"
                     :active-value="1"
                     :inactive-value="0">
          </el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer"
           class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary"
                   @click="onDialogSubmit">确 定</el-button>
      </div>
    </el-dialog>
  </grid>
</template>

<script>
import { mapActions, mapGetters } from 'vuex'
import listMixin from '@/mixin/list'
import { fetchView, store, edit, destory } from './api'
import userPopover from '@/components/common/user-popover'

export default {
  title: '评论管理',
  data () {
    return {
      query: {
        limit: 12,
        page: 1,
        q: undefined,
        status: 'all',
        title: undefined,
        user: undefined
      },
      dialogVisible: false,
      form: {},
      formDefault: {
        id: undefined,
        content: '',
        status: 1
      },
      rules: {
        content: [
          { required: true, message: '名称不能为空' }
        ]
      }
    }
  },
  mixins: [listMixin],
  components: {
    userPopover
  },
  async asyncData ({ store, route }) {
    const { query } = route;
    return store.dispatch('comment/fetchList', { page: 1, limit: 12 })
  },
  computed: {
    ...mapGetters('comment', ['list', 'total'])
  },
  methods: {
    ...mapActions('comment', ['fetchList']),
    fetch () {
      this.fetchList(this.query)
    },
    async editRow (id) {
      await fetchView(id).then(({ data: { data } }) => {
        this.form = this.filterForm(data)
      })
      this.dialogVisible = true
      this.$nextTick(() => this.$refs.dialogForm.clearValidate())
    },
    async deleteRow (id) {
      await this.$confirm('确定删除？')
      await destory(id)
      this.$message.success('删除成功')
      this.fetchList(this.query)
    },
    async onDialogSubmit () {
      await this.$refs.dialogForm.validate()

      if (typeof this.form.id === 'number') {
        await edit(this.form.id, this.form)
      } else {
        await store(this.form)
      }
      this.$message.success('保存成功')
      this.fetchList(this.query)
      this.dialogVisible = false
    }
  }
}
</script>
